import BaseController from '../../core/controllers/Base.controller';
import storageApi from '../../core/storage/storage';

@Inject('$log', '$rootScope', '$scope', 'patrolService', 'ngDialogService')
class NotClosedPatrolController extends BaseController {
  constructor() {
    super(patrolService, 'queryUnclosed');

    this.paginationService = this.patrolService;
    this.$setQueryCallback((res) => {
      this.patrolListData = res.rows;
    });

    this.mapParams.type = '';
    this.selectedIndex = 0;

    this.selectBid = null;
    this.$paginationQuery();
    $scope.$watch('vm.selectBid', (newValue) => {
      if (newValue) {
        this.mapParams.orgId = newValue.orgId;
        this.$paginationQuery();
      }
    });
  }

  $query(param) {
    if (param && param.patrolTimeStart.$valid && param.patrolTimeEnd.$valid
      && param.patrolTimeStart.$viewValue > param.patrolTimeEnd.$viewValue) {
      this.ngDialogService.alertError('巡查时间开始时间应小于结束时间');
      return;
    }
    this.$paginationQuery();
  }

  $reset() {
    this.mapParams.patrolUserName = '';
    this.mapParams.patrolTimeStart = null;
    this.mapParams.patrolTimeEnd = null;
    this.mapParams.type = '';
  }

  $delete(id) {
    this.ngDialogService.confirm('确定删除此数据吗？', id)
      .then((data) => {
        this.$log.debug(data);
        this.patrolService
          .delete([id])
          .then((res) => {
            this.$paginationQuery();
          });
      });
  }
}

export default NotClosedPatrolController;
